package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.UserRequest;
import com.jiyinit.web.core.business.entity.TabCoreOrgEntity;
import com.jiyinit.web.core.business.entity.TabCoreUserEntity;
import com.jiyinit.web.core.business.vo.ContactsVo;
import com.jiyinit.web.core.business.vo.GroupMemberVo;
import com.jiyinit.web.core.business.vo.UserInfoVo;
import com.jiyinit.web.core.common.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.jdbc.Sql;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/11 17:51
 * @since 1.0
 */
@Repository("core.tabCoreUserDao")
public class TabCoreUserDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreUserDao.class);

    private String SQLID = "core.tabCoreUserDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    public int updatePinyin(List<TabCoreUserEntity> list){
        return mybatisDaoContext.batchUpdate(SQLID+"batchUpdatePinyin",list);
    }

    public PagingResult findUsers(PageBounds pageBounds){
        return mybatisDaoContext.findForPage(SQLID+"findUsers",TabCoreUserEntity.class,null,pageBounds);
    }

    public Integer findCount(){
        return mybatisDaoContext.findOne(SQLID+"findUsersCount",Integer.class);
    }

    public List<TabCoreUserEntity> findUsersByOrgList(List<TabCoreOrgEntity> orgList){
        return mybatisDaoContext.findAll(SQLID+"findUsersByOrgList",TabCoreUserEntity.class,orgList);
    }

    public List<TabCoreUserEntity> findUserByOrgId(Integer orgId){
        return mybatisDaoContext.findAll(SQLID+"findUserByOrgId",TabCoreUserEntity.class,orgId);
    }

    public int insert(TabCoreUserEntity tabCoreUser) {
        return mybatisDaoContext.save(SQLID + "insert", tabCoreUser);
    }

    public int selectMaxOrder(Integer orgId){
        return mybatisDaoContext.findOne(SQLID+"selectMaxOrder",Integer.class,orgId);
    }

    public TabCoreUserEntity getUserInfoByLoginName(String loginName) {
        return mybatisDaoContext.findOne(SQLID + "getUserInfoByLoginName", TabCoreUserEntity.class, loginName);
    }

    public PagingResult<UserInfoVo> getUserInfoListByCondiction(UserRequest userRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(userRequest.getLimit());
        pageBounds.setPage(userRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "getUserInfoListByCondiction", UserInfoVo.class, userRequest.toMap(), pageBounds);
    }

    public TabCoreUserEntity getUserInfoById(Integer userId){
        return mybatisDaoContext.findOne(SQLID + "selectByPrimaryKey", TabCoreUserEntity.class, userId);
    }

    public int updateUserInfoById(TabCoreUserEntity tabCoreUser) {
        return mybatisDaoContext.update(SQLID + "updateByPrimaryKey", tabCoreUser);
    }

    public int deleteUserLogicByIds(Integer[] userIds){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userIds", userIds);
        map.put("lastModifyId", UserUtil.getCurrentUser().getUserId());
        map.put("lastModifyTime",new Date());
        return mybatisDaoContext.update(SQLID + "deleteLogicByIds", map);
    }

    
    /**
     * 
         * 按机构ID查询用户
         * @author wuyuefeng
         * @create 2017年4月27日 下午2:21:19
         * @param 
         * @return
     */
    public PagingResult<UserInfoVo> findUsersByOrId(UserRequest userRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(userRequest.getLimit());
        pageBounds.setPage(userRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findUsersByOrId", UserInfoVo.class, userRequest.toMap(), pageBounds);
    }


    public List<ContactsVo> findUsersByCon(List<TabCoreOrgEntity> list,String userName){
        HashMap<String,Object> params = new HashMap<>();
        params.put("orgList",list);
        params.put("userName",userName);
        return mybatisDaoContext.findAll(SQLID+"findUsersByCon",ContactsVo.class,params);
    }
    public List<ContactsVo> getUsers(List<TabCoreOrgEntity> list){
        return mybatisDaoContext.findAll(SQLID + "getUsers",ContactsVo.class,list);
    }

    public List<ContactsVo> findFriends(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findFriends",ContactsVo.class,userId);
    }

    public  List<ContactsVo> queryUsers(String param){
        return mybatisDaoContext.findAll(SQLID+"queryUsers",ContactsVo.class,param);
    }

    public List<GroupMemberVo> findGroupMember(Integer groupId){
        return mybatisDaoContext.findAll(SQLID+"findGroupMember",GroupMemberVo.class,groupId);
    }

    public List<UserInfoVo> findUsersByfunRoleId(List<TabCoreOrgEntity> orgList,Integer funRoleId){
        HashMap<String,Object> params = new HashMap();
        params.put("orgList",orgList);
        params.put("funRoleId",funRoleId);
        return mybatisDaoContext.findAll(SQLID+"findUsersByfunRoleId",UserInfoVo.class,params);
    }

    public List<ContactsVo> findFriendsByCon(Integer userId,String userName){
        HashMap<String,Object> params = new HashMap();
        params.put("userId",userId);
        params.put("userName",userName);
        return mybatisDaoContext.findAll(SQLID+"findFriendsByCon",ContactsVo.class,params);
    }

    public List<ContactsVo> getPartTimeUserInfo(List<TabCoreOrgEntity> orgList){
        return mybatisDaoContext.findAll(SQLID + "getPartTimeUserInfo",ContactsVo.class,orgList);
    }

    /**
     * 修改用户头像
     * @author liuzongguang
     * @create 2018/7/11 10:00
     * @param tabCoreUser
     * @return
     */
    public int updateUserAvatar(TabCoreUserEntity tabCoreUser) {
        return mybatisDaoContext.update(SQLID + "updateUserAvatar", tabCoreUser);
    }

    public List<TabCoreUserEntity> findUsersList(){
        return mybatisDaoContext.findAll(SQLID+"findUsersList",UserInfoVo.class);
    }

    public int updateBySalary(TabCoreUserEntity tabCoreUser) {
        return mybatisDaoContext.update(SQLID + "updateBySalary", tabCoreUser);
    }

    public TabCoreUserEntity getUserInfoByUserCertificateNo(String userName){
        return mybatisDaoContext.findOne(SQLID + "getUserInfoByUserCertificateNo",TabCoreUserEntity.class,userName);
    }

    /**
     *通过父id 获取机构id下的所有用户
     */
    public PagingResult<TabCoreUserEntity> findUserByparentId(UserRequest userRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(userRequest.getLimit());
        pageBounds.setPage(userRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findUserByparentId", TabCoreUserEntity.class, userRequest.toMap(),pageBounds);
    }

    public PagingResult<UserInfoVo> getUserInfoListByUserName(UserRequest userRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(userRequest.getLimit());
        pageBounds.setPage(userRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findUserByUserName", UserInfoVo.class, userRequest.toMap(), pageBounds);
    }
}
